var dataSource;
var grid;

$(document).ready(function () {
    dataSource = new kendo.data.DataSource({
        transport: {
            create: {
                url: appBosch.crud.create,
                dataType: "json"
            },
            read: {
                url: appBosch.crud.read,
                dataType: "json"
            },
            update: {
                url: appBosch.crud.update,
                dataType: "json"
            },
            destroy: {
                url: appBosch.crud.destroy,
                dataType: "json"
            },
            parameterMap: function (options, operation) {
                var params = {};
                switch (operation) {
                    case 'create':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'read':
                        break;
                    case 'update':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'destroy':
                        params = {id: options.models[0].id};
                        break;
                    default:
                        break;
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "id",
                fields: {
                    name: {
                        editable: true,
                        nullable: false,
                        validation: {
                            required: true,
                            min: 1
                        }
                    },
                    attempts: {
                        type: "number",
                        editable: true,
                        nullable: false,
                        validation: {
                            required: true,
                            min: 1
                        },
                        defaultValue: 3
                    },
                    type: {
                        type: "number",
                        editable: true,
                        nullable: false,
                        validation: {
                            required: true,
                            min: 1
                        },
                        defaultValue: appBosch.types[0].value
                    },
                    min_capacity : {
                        type: 'number',
                        defaultValue: 5,
                        editable: true,
                        validation: {
                            required: true,
                            min: 1
                        }
                    },
                    lock_days: {
                        type: "number",
                        editable: true,
                        nullable: false,
                        validation: {
                            required: true,
                            min: 1
                        },
                        defaultValue: 365
                    },
                    minimum_qualification: {
                        type: "number",
                        editable: true,
                        nullable: false,
                        validation: {
                            required: true,
                            min: 1
                        },
                        defaultValue: 100
                    }
                }
            }
        }
    });

    grid = $("#grid").kendoGrid({
        toolbar: [
            {
                name: "create",
                text: 'Agregar curso'
            },
            {
                name: "pdf"
            },
            {
                name: "excel"
            }
        ],
        excel: {
            allPages: true,
            fileName: "Cursos.xlsx",
            filterable: true
        },
        pdf: {
            allPages: true,
            fileName: "Cursos.pdf"
        },
        enabled: false,
        resizable: true,
        columnMenu: true,
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        sortable: true,
        dataSource: dataSource,
        columns: [
            {field: "name", title: "Nombre", width: 350},
            {field: "attempts", title: "Intentos", width: 120},
            {field: "type", title: "Tipo", values: appBosch.types, width: 120},
            {field: "min_capacity", title: "Cupo mínimo", width: 120},
            {field: 'lock_days', title: 'Dias de bloqueo', width: 120},
            {field: 'minimum_qualification', title: 'Calificación mínima', width: 120},
            {
                command: [
                    {text: "Temas", click: showThemes},
                    {
                        name: "edit",
                        text: {
                            edit: "",
                            update: "",
                            cancel: ""
                        }
                    },
                    {
                        name: "destroy",
                        text: ""
                    }], title: "&nbsp;"
            }],
        editable: {
            confirmation: "¿Estas seguro que deseas eliminar este curso?",
            mode: "inline"
        }
    }).data("kendoGrid");
});

function showThemes(e) {
    e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var id = dataItem.id;
    var url = appBosch.urlThemes + '/' + id;
    $(location).attr('href', url);
}

function getTest() {
    if (areas.value !== '0') {
        areaId = areas.value();
        dataSource.transport.options.read.url = appBosch.crud.read + '/' + areas.value();
    } else {
        dataSource.transport.options.read.url = appBosch.crud.read;
    }
    dataSource.read();
}
